package org.wishwork.api.express.send;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wishwork.api.Api;
import org.wishwork.context.Context;
import org.wishwork.helper.HttpHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 称重并发货
 * @author: WangYX
 * @create: 2022-04-13 10:07
 * @Version: 1.0.0
 **/

public class ExpressSendApi extends Api<List<ExpressSendResponse>, List<ExpressSendRequest>> {

    private static final Logger log = LoggerFactory.getLogger(ExpressSendApi.class);

    private Context context;

    public ExpressSendApi(Context context) {
        this.context = context;
    }

    @Override
    protected String query(List<ExpressSendRequest> expressSendRequests) throws Exception {
        String url = "/open/orders/weight/send/upload";
        List<Map<String, Object>> params = new ArrayList<>();
        for (ExpressSendRequest expressSendRequest : expressSendRequests) {
            Map<String, Object> map = new HashMap<>();
            map.put("l_id", expressSendRequest.getLId());
            map.put("weight", expressSendRequest.getWeight());
            if (StrUtil.isNotEmpty(expressSendRequest.getType())) {
                map.put("type", expressSendRequest.getType());
            }
            if (ObjectUtil.isNotEmpty(expressSendRequest.getIsUnLid())) {
                map.put("is_un_lid", expressSendRequest.getIsUnLid());
            }
        }
        String biz = JSON.toJSONString(params);
        return HttpHelper.sendRequest(biz, url);
    }

    @Override
    protected List<ExpressSendResponse> process(JSONObject data, List<ExpressSendRequest> expressSendRequests) throws Exception {
        JSONArray datas = data.getJSONArray("datas");
        return datas.toJavaList(ExpressSendResponse.class);
    }
}
